package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author lvFF
 * @Date 2022/04/26/下午 8:41;
 * Description
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;
    @Override
    public void saveFlaver(DishDto dishDto) {
        log.info("dishDto----------{}",dishDto);
        /*dishDto.setCode("123456789");
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setCreateUser(1L);
        dishDto.setUpdateUser(1L);*/
        //先保存菜品
        this.save(dishDto);
        List<DishFlavor> flavors =dishDto.getFlavors();

        //
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDto.getId()));
        //后保存菜的口味
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void removeByIds(Long[] ids) {
        for (Long id : ids) {
            log.info("id------------{}",id);
            this.removeById(id);
        }
    }

    @Override
    public DishDto getByIdDishFlavor(Long id) {
        Dish byId = this.getById(id);
        DishDto dishDto = new DishDto();
        /*LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(byId.getId()!=null,DishFlavor::getId,byId.getId());
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(new ArrayList<DishFlavor>(list));
        */

        QueryWrapper<DishFlavor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dish_id", byId.getId());
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);

        dishDto.setFlavors(list);


        BeanUtils.copyProperties(byId,dishDto);

        return dishDto;
    }

    @Override
    public Page<DishDto> pageWith(Integer page, Integer pageSize, String name) {
        Page<Dish> dishPage = new Page<>(page, pageSize);
        //
        QueryWrapper<Dish> queryWrapper1 = new QueryWrapper<>();
        //
        queryWrapper1.eq(StringUtils.isNotBlank(name),"name",name);
        //
        queryWrapper1.orderByDesc("update_time");
        //
        this.page(dishPage,queryWrapper1);
        //
        Page<DishDto> dishDtoPage = new Page<>();
        //
        BeanUtils.copyProperties(queryWrapper1,dishDtoPage,"records");
        //

        ArrayList<DishDto> list = new ArrayList<>();
        //
        List<Dish> dishes = dishPage.getRecords();
        //
        for (Dish dish : dishes) {
            DishDto dishDto = new DishDto();
            //
            BeanUtils.copyProperties(dish,dishDto);
            //
            Category category = categoryService.getById(dish.getCategoryId());
            dishDto.setCategoryName(category.getName());
            list.add(dishDto);
        }
        dishDtoPage.setRecords(list);
        return dishDtoPage;
    }


    @Override
    public void getByIdS(Integer s,Long[] ids) {
       // ArrayList<Dish> dishes = new ArrayList<>();
        log.info("ids---------{}",ids);
        for (Long id : ids) {
            //Dish byId = this.getById(id);
            Dish byId = this.getById(id);
            log.info("byId----------{}",byId);
            byId.setStatus(s);
            log.info("s-----------{}",s);

            //byId.setStatus(s);
            log.info("byId----------{}",byId);
            this.updateById(byId);

        }
        //Dish byId = this.getById(ids);
        log.info("byId---------");
        //return dishes;
    }
}
